package com.shu.driftbook.ajax;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SearchServlet extends HttpServlet {
	protected void proccessRequest(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException{
		ServletOutputStream out = response.getOutputStream();
		response.setContentType("text/html;charset=UTF-8");
	        try {  
	            response.setContentType("text/html");
	            response.setHeader("Cache-Control", "no-store");  
	            response.setHeader("Pragma", "no-cache");  
	            response.setDateHeader("Expires", 0);
	            response.setCharacterEncoding("utf-8");
	            out = response.getOutputStream();  
	            Class.forName("com.mysql.jdbc.Driver");
	            Connection con=DriverManager.getConnection("jdbc:mysql://211.101.12.143/drift?useUnicode=true&amp;characterEncoding=UTF-8","root","shuos");
	            Statement stm=con.createStatement();
	            String name = request.getParameter("q");
	            byte[] buff=new byte[512];
	            name=URLDecoder.decode(name,"utf-8");
	            System.out.println(name);
	            if(name!=null) {
	            	name=URLDecoder.decode(name,"utf-8");
	            	name=URLEncoder.encode(name,"ISO-8859-1");
	            	String str="select * from book where bookname like '"+name+"%'"+" or isbn="+"'"+name+"'";
	            	System.out.println(str);
	            	ResultSet rs=stm.executeQuery(str);
	            	while(rs.next()){
	            		InputStream in=rs.getAsciiStream("bookname");
	            		for(;;){
	            			int size=in.read(buff);
	            			if(size==-1)
	            				break;
	            			out.write(buff,0,size);
	            			out.write(new String("<br/>").getBytes());
	            		}
	            	}
	            }  
	        }
	        finally{
	        	out.close();
	        }
	}
	
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			proccessRequest(request,response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			proccessRequest(request,response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
}
